package com.rtp2p.rtcoresdkdemo.replay

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LifecycleOwner
import com.rtp2p.rtcoresdkdemo.base.BaseRecyclerView
import com.rtp2p.rtcoresdkdemo.databinding.ReplayItemBinding
import com.rtp2p.rtp2pcore.utils.RTTimeUtils
import com.rtp2p.rtp2pcore.common.RTCallback
import com.rtp2p.rtp2pcore.core.TFCardRecordsRequest

class ReplayAdapter(private val replay: RTCallback<TFCardRecordsRequest.RecordFile>) :
    BaseRecyclerView.BaseAdapter<TFCardRecordsRequest.RecordFile, ReplayAdapter.ReplayListVH>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReplayListVH {
        val binding = ReplayItemBinding.inflate(LayoutInflater.from(parent.context))
        binding.lifecycleOwner = parent.context as LifecycleOwner
        return ReplayListVH(binding)
    }

    override fun onBindViewHolder(holder: ReplayListVH, position: Int) {
        getData(position)?.let { holder.update(it) }
    }

    inner class ReplayListVH(binding: ReplayItemBinding) :
        BaseRecyclerView.BaseViewHolder<TFCardRecordsRequest.RecordFile, ReplayItemBinding>(binding) {
        override fun onChange(data: TFCardRecordsRequest.RecordFile) {
            binding.name = data.name
            binding.startTime = "开始时间：${RTTimeUtils.getPTZByTime(data.startTime)}"
            binding.totalTime = "时长：${data.totalTime}s"
            binding.root.setOnClickListener {
                replay.accept(data)
            }
        }
    }
}